package com.liurong.quartz.action;

import java.util.Map;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.liurong.quartz.util.JobDetailWork;
import com.liurong.quartz.util.JobStartConfig;
import com.liurong.quartz.util.bean.BeanUtil;
import com.liurong.quartz.util.entity.JobEntity;

/**
 * 定时任务控制类
 * 
 * @author liurong
 *
 */
@Controller
@RequestMapping("quartz")
public class QuartzController {

	/**
	 * 获取所有的定时任务里列表
	 * 
	 * @return
	 */
	@RequestMapping("scheduler_list")
	public ModelAndView getSchedulerList() {
		System.out.println("获取当前的定时任务-------------");
		Map<String, JobEntity> jobnamemap = JobStartConfig.jobNameMap;
		System.out.println("当前的定时任务有：" + jobnamemap);
		ModelAndView mav = new ModelAndView("scheduler_list");
		mav.addObject("jobNameMap", jobnamemap);
		return mav;
	}

	/**
	 * 暂定定时任务
	 * 
	 * @param jobName
	 *            定时任务名称
	 * @return
	 * @throws SchedulerException
	 */
	@RequestMapping("stop")
	public ModelAndView stopJob(String jobName) throws SchedulerException {
		System.out.println("暂停定时任务:**********" + jobName);
		Scheduler scheduler = JobStartConfig.scheduler;
		if (scheduler == null) {
			System.out.println("定时任务并没有启动成功！");
			return new ModelAndView("redirect:/quartz/scheduler_list.do");
		}
		scheduler.pauseJob(new JobKey(jobName, Scheduler.DEFAULT_GROUP));

		Map<String, JobEntity> jobnamemap = JobStartConfig.jobNameMap;
		JobEntity jobEntity = jobnamemap.get(jobName);
		jobEntity.setIsStart(0);
		jobnamemap.put(jobName, jobEntity);

		return new ModelAndView("redirect:/quartz/scheduler_list.do");
	}

	/**
	 * 启动定时任务
	 * 
	 * @param jobName
	 *            定时任务名称
	 * @return
	 * @throws SchedulerException
	 */
	@RequestMapping("start")
	public ModelAndView startJob(String jobName) throws SchedulerException {
		System.out.println("启动定时任务：**********" + jobName);
		Scheduler scheduler = JobStartConfig.scheduler;
		if (scheduler == null) {
			System.out.println("定时任务并没有启动成功！");
			return new ModelAndView("redirect:/quartz/scheduler_list.do");
		}
		scheduler.resumeJob(new JobKey(jobName, Scheduler.DEFAULT_GROUP));

		Map<String, JobEntity> jobnamemap = JobStartConfig.jobNameMap;
		JobEntity jobEntity = jobnamemap.get(jobName);
		jobEntity.setIsStart(1);
		jobnamemap.put(jobName, jobEntity);

		return new ModelAndView("redirect:/quartz/scheduler_list.do");
	}

	/**
	 * 手动启动定时任务
	 * 
	 * @param jobName
	 * @return
	 * @throws SchedulerException
	 */
	@RequestMapping("handStart")
	public ModelAndView handStartJob(String jobName) throws SchedulerException {
		System.out.println("手动启动定时任务：**********" + jobName);

		Map<String, JobEntity> jobnamemap = JobStartConfig.jobNameMap;
		JobEntity jobEntity = jobnamemap.get(jobName);
		if (jobEntity == null) {
			System.out.println("传入的jobName有误！");
			return new ModelAndView("redirect:/quartz/scheduler_list.do");
		}

		if (!jobEntity.isHandStart()) {
			System.out.println("该定时任务不支持手动执行！");
			return new ModelAndView("redirect:/quartz/scheduler_list.do");
		}

		JobDetailWork bean = (JobDetailWork) BeanUtil.getBean(jobName);
		if (bean == null) {
			System.out.println("传入的jobName有误！");
			return new ModelAndView("redirect:/quartz/scheduler_list.do");
		}
		bean.work();

		return new ModelAndView("redirect:/quartz/scheduler_list.do");
	}

	/**
	 * 动态修改cron表达式
	 * 
	 * @param jobName
	 * @param cronExper
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("change_cron")
	public ModelAndView changeCron(String jobName, String cronExper) {
		System.out.println("修改执行时间：jobName=" + jobName + ",cronExper=" + cronExper);
		try {
			Scheduler scheduler = JobStartConfig.scheduler;

			Map<String, JobEntity> jobnamemap = JobStartConfig.jobNameMap;
			JobEntity jobEntity = jobnamemap.get(jobName);
			String oldTime = jobEntity.getCronStr();

			if (!oldTime.equalsIgnoreCase(cronExper)) {
				// 触发器
				TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
				// 触发器名,触发器组
				triggerBuilder.withIdentity(jobName, Scheduler.DEFAULT_GROUP);
				triggerBuilder.startNow();
				// 触发器时间设定
				triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cronExper));
				// 创建Trigger对象
				CronTrigger trigger = (CronTrigger) triggerBuilder.build();

				TriggerKey triggerKey = new TriggerKey(jobName, Scheduler.DEFAULT_GROUP);
				scheduler.rescheduleJob(triggerKey, trigger);

				jobEntity.setIsStart(1);
				jobEntity.setCronStr(cronExper);
			}
		} catch (SchedulerException e) {
			System.out.println("更改执行时间抛出异常" + e);
		}
		return new ModelAndView("redirect:/quartz/scheduler_list.do");
	}
}
